<script>
	import Vue from 'vue';
	import store from './store';
	import TIM from 'tim-js-sdk';
	import TIMUploadPlugin from 'tim-upload-plugin';
	import COS from 'cos-js-sdk-v5';
	import tim from './common/utils/tim.js';
	// Vue.prototype.$tim = tim; // 全局可用
	export default {
		onLaunch: function() {
			// 设置全局消息监听
			// tim.on(TIM.EVENT.SDK_READY, this.handleSDKReady);
			// tim.on(TIM.EVENT.MESSAGE_RECEIVED, this.handleMessage);
			this.reloginTIM();
			// #ifdef APP-PLUS
			if (plus.os.name === 'Android') {
				// Android 禁止截屏
				const mainActivity = plus.android.runtimeMainActivity();
				const Window = plus.android.importClass('android.view.Window');
				const window = mainActivity.getWindow();
				window.addFlags(Window.LayoutParams.FLAG_SECURE);
			} else if (plus.os.name === 'iOS') {
				// iOS 监听截屏（无法阻止，但可以提示）
				plus.key.addEventListener('capture', () => {
					uni.showToast({
						title: '禁止截屏',
						icon: 'none'
					});
				});
			}
			// #endif
			document.addEventListener('keydown', (e) => {
				if (e.key === 'PrintScreen' || (e.ctrlKey && e.key === 'p')) {
					e.preventDefault();
					alert('禁止截屏或打印');
				}
			});
		},
		methods: {
			handleSDKReady(event) {
				// SDK准备就绪
				store.commit('im/updateSDKReady', true);
			},
			handleMessage(event) {
				// 处理收到的消息
				event.data.forEach(message => {
					// 可以存入Vuex或触发通知
				});
			},
			// 重新登录腾讯IM
			async reloginTIM(){
				try {
					const userID = uni.getStorageSync('user_info').id;
					const userSig = uni.getStorageSync('signIM');;
					console.log(11,userID)
					if (userSig && userID) {
						const imResponse = await tim.login({
							userID: userID.toString(),
							userSig: userSig
						});
					}
				} catch (error) {
					console.error('重新登录腾讯IM时发生错误', error);
				}
				tim = TIM;
				const SDKAppID = 1600102039;
				uni.$aegis.reportEvent({
						name: 'onLaunch',
						ext1: 'onLaunch-success',
						ext2: 'uniTuikitExternal',
						ext3: `${SDKAppID}`,
				})
				tim = TIM.create({
					SDKAppID: SDKAppID
				});
				tim.registerPlugin({'tim-upload-plugin': TIMUploadPlugin });
				tim.registerPlugin({'cos-js-sdk': COS}); 
				timEvent = TIM.EVENT;
				tim.on(timEvent.KICKED_OUT, this.onKickedOut);
			},
		},
		onShow: function() {
			// 页面显示时尝试重新登录腾讯IM
			
		},
		onHide: function() {
			// console.log('App Hide')
		}
	}
</script>

<style>
	/*每个页面公共css */
	@import '/static/css/base.css';

	/* 隐藏容器滚动条 */
	.uni-scroll-view::-webkit-scrollbar {
		display: none;
	}


	/* 隐藏滚动条 */
	.b-hideScroll::-webkit-scrollbar {
		display: none;
	}

	/* 单行溢出隐藏 */
	.b-oneline {
		overflow: hidden;
		text-overflow: ellipsis;
		white-space: nowrap;
	}

	/* 两行溢出隐藏 */
	.b-twoline {
		white-space: normal;
		word-break: break-all;
		overflow: hidden;
		text-overflow: ellipsis;
		display: -webkit-box;
		-webkit-box-orient: vertical;
		-webkit-line-clamp: 2;
	}
</style>